package com.qcby.mycommunity_003.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qcby.mycommunity_003.entity.Log;
import com.qcby.mycommunity_003.form.LogForm;
import com.qcby.mycommunity_003.mapper.LogMapper;
import com.qcby.mycommunity_003.service.LogService;
import com.qcby.mycommunity_003.vo.LogVo;
import com.qcby.mycommunity_003.vo.PageVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 操作日志表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2024-04-03
 */
@Service
public class LogServiceImpl extends ServiceImpl<LogMapper, Log> implements LogService {

    @Autowired
    private LogMapper logMapper;
    @Override
    public PageVo getlist(LogVo logVo) {
        Page<Log> page=new Page<>();
        page.setCurrent(logVo.getPage());
        page.setSize(logVo.getLimit());
        QueryWrapper<Log> queryWrapper=new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(logVo.getUsername()),"username",logVo.getUsername())
                .eq(StringUtils.isNotBlank(logVo.getOperation()),"operation",logVo.getOperation())
                .between(StringUtils.isNotBlank(logVo.getStartDate())||StringUtils.isNotBlank(logVo.getEndDate()),"create_time",logVo.getStartDate(),logVo.getEndDate());
        Page<Log> logPage = logMapper.selectPage(page, queryWrapper);
        PageVo pagevo=new PageVo();
        pagevo.setTotalCount(logPage.getTotal());
        pagevo.setPageSize(logPage.getSize());
        pagevo.setTotalPage(logPage.getPages());
        pagevo.setCurrPage(logPage.getCurrent());
        List<LogForm> list=new ArrayList<>();
        for (Log log1 : logPage.getRecords()) {
            LogForm logForm=new LogForm();
            BeanUtils.copyProperties(log1,logForm);
            logForm.setLogId(log1.getLogId());
            list.add(logForm);
        }
        pagevo.setList(list);
        return pagevo;
    }
}
